package org.lecric.wind44.client.widget;

import org.lecric.wind44.client.model.Board;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class BoardShapeComparator
{
	private static final int CANVA_LENGTH = 300;

	private VerticalPanel vpanel=new VerticalPanel();
	
	private Canvas canva;

	private Image image2;

	private Image image;

	private ImageListener listener = new ImageListener() {
		@Override
		public void onLoadingComplete(Image image) {
			drawImages();
		}
	};

	private ListBox listBox1;
	private ListBox listBox2;

	private RadioButton[] radioButtons;

	public BoardShapeComparator() {
		listBox1 = new ListBox();
		init(listBox1);
		listBox1.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				image= new Image(listBox1.getValue(listBox1.getSelectedIndex()), listener);
			}
		});
		listBox2 = new ListBox();
		init(listBox2);
		listBox2.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				image2= new Image(listBox2.getValue(listBox2.getSelectedIndex()), listener);
			}
		});
		canva = new Canvas(800, 800);
		canva.addMouseMoveHandler(new MouseMoveHandler() {
			@Override
			public void onMouseMove(MouseMoveEvent event) {
				drawImages(event.getX(), event.getY());
			}
		});
		HorizontalPanel slider=new HorizontalPanel();
		radioButtons = new RadioButton[5];
		for(int i=0 ; i < radioButtons.length; i++)
		{
			radioButtons[i]=new RadioButton("transparency");
			radioButtons[i].addValueChangeHandler(new ValueChangeHandler<Boolean>() {
				@Override
				public void onValueChange(ValueChangeEvent<Boolean> event) {
					if(event.getValue())
						drawImages();
				}
			});
			slider.add(radioButtons[i]);
			
		}
		radioButtons[2].setValue(true);
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(listBox1);
		hpanel.add(slider);
		hpanel.add(listBox2);
		vpanel.add(hpanel);
		vpanel.add(canva);
	}
	public Widget getWidget()
	{
		return vpanel;
	}
	

	private void init(ListBox listBox) {
		listBox.addItem("", "");
		for(Board board: Board.values())
			listBox.addItem(board.getGuiName(), board.getUri());
	}

	protected void drawImages(int i, int j) {
		canva.setGlobalAlpha(1.0);
		canva.setFillStyle("white");
		canva.fillRect(0.0, 0.0, canva.getWidth(), canva.getHeight());
		if(image!=null && image.isLoaded())
		{
			Board board1=Board.values()[listBox1.getSelectedIndex()-1];
			drawImage(image, board1, canva.getWidth()/2, canva.getHeight()/2);
//			canva.drawImage(image, 0.0, 0.0, canva.getWidth(), canva.getHeight());
			if(image2!=null && image2.isLoaded())
			{
				Board board2=Board.values()[listBox2.getSelectedIndex()-1];
				for(int k=0,l=20; k < radioButtons.length; k++,l+=15)
				{
					if(radioButtons[k].getValue())
						canva.setGlobalAlpha((l)/100.0);
				}
//				canva.drawImage(image2, i, j,
//						canva.getWidth(), canva.getHeight());
				drawImage(image2, board2, i, j);
			}
		}
	}

	private void drawImage(Image image, Board board, int centerX, int centerY)
	{
//		int imageHeight=image.getHeight();
		int canvaHeight=canva.getHeight();
		int boardLength=board.getLength();
		int imageHeight=canvaHeight*boardLength/CANVA_LENGTH;
//		double ratio=image.getHeight()/imageHeight;
		int imageWidth=image.getWidth()*imageHeight/image.getHeight();
		canva.drawImage(image, centerX-imageWidth/2, centerY-imageHeight/2,
				imageWidth, imageHeight);
	}

	private void drawImages() {
		drawImages(canva.getWidth()/2, canva.getHeight()/2);
	}
}
